﻿// -----------------------------------------------------------------------
// <copyright file="NodeData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Node
    /// </summary>
    public class NodeData
    {
        /// <summary>
        /// Get Node By Id
        /// </summary>
        /// <param name="nodeId">Param nodeId</param>
        /// <returns>Return Node</returns>
        public NodeEN GetNodeById(int nodeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeEN node = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeGetById, nodeId);
                while (reader.Read())
                {
                    node = new NodeEN();

                    if (!reader.IsDBNull(0))
                    {
                        node.NodeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        node.NodeTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        node.Code = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        node.Name = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        node.Orders = reader.GetInt32(4);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return node;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Node
        /// </summary>
        /// <returns>Return list all Node</returns>
        public IList<NodeEN> GetNodes()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeEN> lstNode = new List<NodeEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeGetAll);
                while (reader.Read())
                {
                    NodeEN node = new NodeEN();

                    if (!reader.IsDBNull(0))
                    {
                        node.NodeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        node.NodeTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        node.Code = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        node.Name = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        node.Orders = reader.GetInt32(4);
                    }

                    lstNode.Add(node);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNode;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Node
        /// </summary>
        /// <param name="node">Paarm Node</param>
        /// <returns>Insert Node</returns>
        public int Insert(NodeEN node)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Node.NodeTypeId, node.NodeTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.Node.Code, node.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Node.Name, node.Name),
                                    new SqlParameter(Constants.SqlParamerterName.Node.Orders, node.Orders)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Node
        /// </summary>
        /// <param name="node">Param Node</param>
        public void Update(NodeEN node)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Node.NodeId, node.NodeId),
                                    new SqlParameter(Constants.SqlParamerterName.Node.NodeTypeId, node.NodeTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.Node.Code, node.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Node.Name, node.Name),
                                    new SqlParameter(Constants.SqlParamerterName.Node.Orders, node.Orders)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Node by nodeId
        /// </summary>
        /// <param name="nodeId">param nodeId</param>
        public void Delete(int nodeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Node.NodeId, nodeId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List Node By NodeTypeId
        /// </summary>
        /// <param name="nodeTypeId">Param nodeTypeId</param>
        /// <returns>Return List Node</returns>
        public IList<NodeEN> GetNodeByNodeTypeId(int nodeTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeEN> lstNode = new List<NodeEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeGetByNodeTypeId, nodeTypeId);
                while (reader.Read())
                {
                    NodeEN node = new NodeEN();

                    if (!reader.IsDBNull(0))
                    {
                        node.NodeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        node.NodeTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        node.Code = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        node.Name = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        node.Orders = reader.GetInt32(4);
                    }

                    lstNode.Add(node);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNode;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Check exist code
        /// </summary>
        /// <param name="id">Param id</param>
        /// <param name="code">Param code</param>
        /// <returns>Return true or false</returns>
        public bool Exist(int id, string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeExist, id, code));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}